//
//  AY_BookCityPage.m
//  AiYue
//
//  Created by boWen on 2023/10/16.
//

#import "AY_BookCityPage.h"
#import "ZJScrollPageView.h"
#import "AY_BookManPage.h"
#import "AY_SearchViewController.h"
@interface AY_BookCityPage ()<ZJScrollPageViewDelegate, SXLoadingDelegate>
@property (strong, nonatomic) NSArray<NSString *> *titles;
@property (strong, nonatomic) NSArray<SortModel *> * channelList;
@property (strong, nonatomic) NSArray<UIViewController *> *childVcs;
@property (nonatomic, strong) ZJScrollPageView *scrollPageView;
@property (strong,nonatomic) UIView *searchView;

@end

@implementation AY_BookCityPage

- (void)viewDidLoad {
    [super viewDidLoad];
    self.gk_navigationBar.hidden = YES;
    [self getConfigData];
}


- (void)getConfigData {
    [AYApiRequest getMallChannelSuccess:^(AY_titleSModel * _Nonnull pkg) {
        if([pkg.code isEqualToString:@"200"]) {
            
            NSMutableArray * titleAry = [@[] mutableCopy];
            NSMutableArray * sortNoAry = [@[] mutableCopy];
            if (pkg.data.count > 0) {
                self.channelList = pkg.data;
                for (SortModel *sorModel in pkg.data) {
                    if (!kStringIsEmpty(sorModel.sortNo)) {
                        [sortNoAry addObject:[NSNumber numberWithInt:[kStringUtils(sorModel.sortNo) intValue]]];
                    }
                }
                //先排序处理数据
                sortNoAry = [NSMutableArray arrayWithArray:[AYTool sortASC:sortNoAry]];
                if(sortNoAry.count > 0) {
                    for (int i=0; i<sortNoAry.count; i++) {
                        for (SortModel *sorModel in pkg.data) {
                            if([[sortNoAry[i] stringValue] isEqualToString:sorModel.sortNo]) {
                                [titleAry addObject:sorModel.name];
                            }
                        }
                    }
                }
            }
            NSLog(@"----titleAry--%@", titleAry);
            self.titles = titleAry;
        }
        [self setInitScrollerPageView];
    } failure:^(int status, NSString * _Nonnull info) {
        ShowToast(@"亲，网络不顺畅哟~");
    }];
}


- (void)setInitScrollerPageView {
    ZJSegmentStyle *style = [[ZJSegmentStyle alloc] init];
    style.gradualChangeTitleColor = YES;
    style.showLine = YES;
    style.selectedTitleColor = COLOR(@"#000002");
    style.normalTitleColor = COLOR(@"#878592");
    style.scrollLineColor = THEME_COLOR;
    style.scrollLineHeight = YNUM(3);
    style.scaleTitle = YES;
    style.titleMargin = 30;
    style.adjustCoverOrLineWidth = YES;
    style.titleFont = [UIFont systemFontOfSize:YNUM(15) weight:UIFontWeightMedium];
    _scrollPageView = [[ZJScrollPageView alloc] initWithFrame:CGRectMake(0, GK_STATUSBAR_HEIGHT, self.view.bounds.size.width , self.view.bounds.size.height - GK_STATUSBAR_HEIGHT) segmentStyle:style titles:self.titles parentViewController:self delegate:self];
    [self.view addSubview:_scrollPageView];
    [self.view addSubview:self.searchView];
}


- (UIView *)searchView {
    if (!_searchView) {
        UIView *searchView = [AYTool createViewWithFrame:CGRectMake(self.view.bounds.size.width - YNUM(190), GK_STATUSBAR_HEIGHT, YNUM(172), YNUM(34)) andBg:COLOR(@"#EAEEF7") andRadius:YNUM(16) andAddSubView:self.view];
        UIImageView *icon = [AYTool createImageViewWithFrame:CGRectZero andImage:YImage(@"AY_search_icon") andBg:nil andRadius:0 andAddSubView:searchView];
        [icon mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(searchView).offset(YNUM(12));
            make.centerY.equalTo(searchView);
            make.width.height.mas_equalTo(YNUM(16));
        }];
        UILabel *info = [AYTool createLabelWithFrame:CGRectZero andText:@"搜索书名、作者" andTextColor:COLOR(@"#8A8E97") andBgColor:nil andFont:YFont(14) andLines:1 andRadius:0 andAddSubView:searchView];
        [info mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(icon.mas_right).offset(YNUM(8));
            make.centerY.equalTo(searchView);
        }];
        
        UIButton *btn = [[UIButton alloc] initWithFrame:searchView.bounds];
        [btn addTarget:self action:@selector(searchCityBook) forControlEvents:UIControlEventTouchUpInside];
        [searchView addSubview:btn];
        _searchView = searchView;
    }
    return _searchView;
}

- (void)refreshAction {
    [self getConfigData];
}

- (void)searchCityBook {
    AY_SearchViewController *searchVC = [[AY_SearchViewController alloc] init];
    [self.navigationController pushViewController:searchVC animated:YES];
}

#pragma ZJScrollPageViewDelegate 代理方法
- (NSInteger)numberOfChildViewControllers {
    return self.titles.count;
}

- (UIViewController<ZJScrollPageViewChildVcDelegate> *)childViewController:(UIViewController<ZJScrollPageViewChildVcDelegate> *)reuseViewController forIndex:(NSInteger)index {
    AY_BookManPage<ZJScrollPageViewChildVcDelegate> *childVc = (AY_BookManPage*)reuseViewController;
    
    if (childVc == nil) {
        childVc = [[AY_BookManPage alloc] init];
        if (self.channelList.count > index) {
            childVc.channelId = kStringUtils([self.channelList[index] channelId]);
        }
    }
    return childVc;
}
- (BOOL)shouldAutomaticallyForwardAppearanceMethods {
    return NO;
}

@end
